package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.AuthAdminContent;
import com.xmy.cultivate.entity.PracticeBrushTeacher;
import com.xmy.cultivate.service.IAuthAdminContentService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 内容【数据】权限 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/api/admin/authAdminContent")
public class AuthAdminContentController extends BaseController{

    @Autowired
    IAuthAdminContentService iPracticeBrushTeacherService;

    @PostMapping("/saveAuthAdminContent")
    public Result saveAuthAdminContent(@RequestBody AuthAdminContent authAdminContent) {

        if (authAdminContent.getId() == null){
            boolean save = iPracticeBrushTeacherService.save(authAdminContent);
            if (save) {
                return new Result(Code.OK, "", "添加成功");
            }
            return new Result(Code.ERROR,"","添加失败");
        }else{
            boolean rs;
            if (StringUtils.isEmpty(authAdminContent.getSchoolIds()) && StringUtils.isEmpty(authAdminContent.getSubjectsIds()) && StringUtils.isEmpty(authAdminContent.getYearClassIds())){
                rs = iPracticeBrushTeacherService.removeById(authAdminContent.getId());
            }else {
                rs = iPracticeBrushTeacherService.updateById(authAdminContent);
            }
            if (rs) {
                return new Result(Code.OK, "", "修改成功");
            }else {
                return new Result(Code.ERROR,"","修改失败");
            }

        }

    }

    @GetMapping("/getAuthAdminContentList")
    public Result getAuthAdminContentList(@RequestParam(value = "teacherId", required = true) String teacherId) {
        if (StringUtils.isEmpty(teacherId)){
            return new Result(Code.ERROR,"","请上传teacherId");
        }
        QueryWrapper<AuthAdminContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        List<AuthAdminContent> authAdminContentList = iPracticeBrushTeacherService.list(queryWrapper);

        return new Result(Code.OK, authAdminContentList, Code.GET_OK_MSG);

    }
}
